<template>
  <el-form :model="form" ref="form" :rules="rules" class="form">
    <el-form-item class="form-item" prop="username">
      <el-input
        placeholder="用户名/手机"
        v-model="form.username"
        @focus="hideErrMsg('username')"
      >
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="captcha">
      <el-input
        placeholder="验证码"
        v-model="form.captcha"
        @focus="hideErrMsg('captcha')"
      >
        <template slot="append">
          <el-button @click="handleSendCaptcha"> 发送验证码 </el-button>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="nickname">
      <el-input
        placeholder="昵称"
        v-model="form.nickname"
        @focus="hideErrMsg('nickname')"
      >
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="password">
      <el-input
        placeholder="密码"
        type="password"
        v-model="form.password"
        @focus="hideErrMsg('password')"
      ></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="checkPassword">
      <el-input
        placeholder="确认密码"
        type="password"
        v-model="form.checkPassword"
        @focus="hideErrMsg('checkPassword')"
      >
      </el-input>
    </el-form-item>
    <el-button class="submit" type="primary" @click="handleRegSubmit">
      注册
    </el-button>
  </el-form>
</template>

<script>
export default {
  data() {
    // 确认密码
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      // 表单数据
      form: {
        username: "", // 登录用户名/手机
        password: "", // 登录密码
        nickname: "", // 昵称
        captcha: "", // 手机验证码
        checkPassword: "", // 确认密码
      },
      // 表单规则
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 3,
            max: 11,
            message: "长度在 3 到 11 个字符",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
        checkPassword: [
          {
            validator: validatePass,
            trigger: "blur",
          },
        ],
        nickname: [
          {
            required: true,
            message: "请输入昵称",
            trigger: "blur",
          },
        ],
        captcha: [
          {
            required: true,
            message: "请输入验证码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    handleSendCaptcha() {
      if (!this.form.username || this.form.username.length != 11) {
        this.$message.error("请输入正确手机号码");
        return;
      }
      this.$axios({
        method: "post",
        url: "/captchas",
        data: {
          tel: this.form.username,
        },
      }).then((res) => {
        console.log(res.data);
        if (res.data.code) {
          this.$message.success("验证码发送成功: " + res.data.code);
        }
      });
    },
    handleRegSubmit() {
      console.log(this.form);
      //发送前做一次表单校验
      this.$refs.form
        .validate()
        .then((res) => {
          console.log("校验成功");
          // 先用剩余参数语法将 checkPassword 排除掉
          const { checkPassword, ...data } = this.form;
          this.$axios({
            method: "post",
            url: "/accounts/register",
            data,
          })
            .then((res) => {
              console.log(res.data);
              this.$message.success("注册成功");
              // 自动登录, 将数据存放到 vuex 里面
              this.$store.commit("user/setUserInfo", res.data);
            })
            .catch((err) => {
              console.dir(err);
            });
        })
        .catch((err) => {
          console.log("校验失败");
        });
    },
    hideErrMsg(propName) {
      this.$refs.form.clearValidate(propName);
    },
  },
};
</script>

<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>
